﻿<!DOCTYPE html>
<html>
<head>
    <title>文档列表</title>
    $css()
    $iconcss()
    <style type="text/css">
        #dg td {
            padding: 3px 10px;
        }

        #dg b {
        }

        .ui-datagrid-header {
            display: none;
        }

        dl dt {
            float: left;
        }
        .submitbar {
            padding: 10px 0 30px 60px;
        }
    </style>
</head>
<body>
    <div class="overhidden">
        <table cellpadding="0" cellspacing="0" width="100%" height="100%" style="margin:0;padding:0;">
            <tr>
                <td width="350px" class="control" valign="top">
                    <div class="top">
                        <ul>
                            <li><a href="javascript:;" class="fn" fn="create">$icon(create)添加</a></li>
                            <!-- <li><a href="javascript:;" class="fn" fn="del">$icon(delete)删除</a></li>-->
                        </ul>
                    </div>
                    <div class="main" id="dg"></div>
                    <div class="ui-table-meta" id="pager">共-条</div>
                </td>
                <td valign="top">
                    <div id="pl" class="relative autoHeight autoScroll"></div>
                </td>
            </tr>
        </table>
    </div>
    <script type="text/template" id="list_menu_tpl">
        <div class="item" onclick="menu.add()">
            <a href="javascript:;">$icon(create)新建文档</a>
        </div>
        <div class="item" onclick="menu.refresh()">
            <a href="javascript:;">$icon(refresh)刷新列表</a>
        </div>
    </script>

    <script type="text/template" id="tpl">
        <div class="item" onclick="menu.add()">
            <a href="javascript:;">$icon(create)新建文档</a>
        </div>
        <div class="item" onclick="menu.edit('%id%')">
            <a href="javascript:;">$icon(edit)编辑</a>
        </div>

        <div class="item" onclick="menu.forword('%id%')">
            <a href="javascript:;">$icon(comment)转发</a>
        </div>

        <div class="item" onclick="menu.republish('%id%')">
            <a href="javascript:;">$icon(refresh)重新发布</a>
        </div>
        <div class="item" onclick="menu.del('%id%')">
            <a href="javascript:;">$icon(delete)删除</a>
        </div>

        <div class="item" onclick="menu.comment('%id%')">
            <a href="javascript:;">$icon(comment)评论管理</a>
        </div>


        <div class="item" onclick="menu.relatedlink('%id%')">
            <a href="javascript:;">$icon(link)关联链接</a>
        </div>
        <div class="item" onclick="menu.refresh()">
            <a href="javascript:;">$icon(refresh)刷新列表</a>
        </div>
    </script>
    <script type="text/template" id="archive_item_template">
        <div valign="middle" style="overflow:hidden-x;width:100%">
            <span class="index">{index}</span>
            <span title="{title}">{title}</span>
            <br>
            <input class="ck" type="checkbox" style="border:none">
            <span style="color:#666">{alias}</span>
            <span class="time">{publish}</span>
        </div>
    </script>

    $js()

    <script type="text/javascript">

        cms.dom.fitHeight(document.body.getElementsByTagName('DIV')[0]);
        var itemTemplate = cms.$('archive_item_template').innerHTML;

        var pagerPram = {
            category_id: '${categoryid}',
            author: '',
            page_size: 10,
            page_index: 1,
            lb_visible: -1,
            lb_special: -1,
            lb_system: -1,
            lb_page: -1
        };

        pagerPram.page_size = parseInt((document.documentElement.clientHeight - 40) / 45);

        var _dg = cms.grid('dg', {
            url: '?module=archive&action=pagerJsonData',
            data: pagerPram,
            idField: 'id',
            columns: [
                {
                    field: 'title', title: '名称', width: 200, formatter: function (val, row, index) {
                        return cms.template(itemTemplate, {
                            index: (index + 1),
                            title: val.length > 25 ? val.substring(0, 25) + '...' : val,
                            publish: row.createDate.replace('T', ' '),
                            alias: row.alias ? '(别名：' + row.alias + ')' : ''
                        });
                    }
                }
            ],
            loaded: function (data) {
                var tables = document.getElementsByTagName('TABLE');

                var tds = tables[2].getElementsByTagName('TD');

                var allowMultSelect = false;

                J.table.dynamic(tables[2], allowMultSelect, function (trs) {
                    var id = trs[0].getAttribute('data-indent');
                    cms.load2('pl', 'archive', 'view', 'archive.id=' + id);
                });

                cms.each(tds, function (i, e) {
                    cms.contextmenu.bind(e, null, function (menu) {
                        menu.innerHTML = cms.template(cms.$('tpl').innerHTML, { id: e.parentNode.getAttribute('data-indent') });
                    });
                });


                cms.each(cms.getElementsByClassName(document.body, 'ui-datagrid-view'), function (i, e) {
                    var tbs = e.getElementsByTagName('TABLE');
                    if (tbs.length != 0) {
                        tbs[0].className = 'ui-table ui-table-list';
                    }
                });


                //加载分页
                this.panel.nextSibling.innerHTML = data.pager;

                if (cms.$('pl').innerHTML == '' && data.rows.length != 0) {
                    cms.load2('pl', 'archive', 'view', 'archive.id=' + data.rows[0].id);
                }
            }
        });

        _dg.resize();

        function toPage(i) {
            pagerPram.page_index = i;
            _dg.reload(pagerPram);
        }


        //创建
        Fn.create = function () {
            cms.load2('pl', 'archive', 'create', 'category.id=${categoryid}', initEditor);
        };


        //绑定列表菜单
        cms.contextmenu.bind(cms.$('dg').parentNode, cms.$('list_menu_tpl').innerHTML);


        var menu = {
            refresh: function () {
                _dg.reload();
            },
            add: Fn.create,

            //编辑文档
            edit: function (id) {
                cms.load2('pl', 'archive', 'update', 'archive.id=' + id, initEditor);
            },

            //删除文档
            del: function (id) {
                if (confirm('确定删除吗?删除后将不可恢复!')) {
                    cms.xhr.jsonPost('?', 'module=archive&action=delete&archive.id=' + id,
                    function (json) {
                        if (json.result) {
                            cms.$('pl').innerHTML = '';
                            _dg.reload();
                        }
                        else {
                            parent.M.alert(json.message);
                        }
                    }
                    );
                }
            },
            forword:function(id){
                var d = cms.dialog.create2("文档转发", true, true);
                d.open("?module=archive&action=forword&archive.id=" + id,600,400,false);
            },
            //评论管理
            comment: function (id) {
                cms.load2('pl', 'archive', 'comment', 'desc=true&archive.id=' + id);
            },

            //重新发布
            republish: function (id) {
                cms.xhr.jsonPost('?', 'module=archive&action=republish&archive.id=' + id,
                  function (json) {
                      if (json.result) {
                          parent.M.alert('已经刷新最后更新时间！');
                          _dg.reload();
                      } else {
                          parent.M.alert(json.message);
                      }
                  });
            },
            relatedlink: function (id) {
                cms.$('pl').innerHTML = '<iframe frameborder="0" scrolling="no" width="100%" height="100%" ' +
                    'src="?module=link&action=relatedlink&contentId=' + id + '&type=archive"></ifarme>';
            }
        };
    </script>

    <!-- 编辑器 -->
    <script type="text/javascript" src="/framework/assets/editor/minpack.js"></script>
    <script type="text/javascript" src="/framework/assets/editor/plugins/code/prettify.js"></script>
    <script type="text/javascript">
        var editor = null;
        function initEditor() {
            editor = KindEditor.create('#editor', {
                cssPath: '/framework/assets/editor/plugins/code/prettify.css',
                uploadJson: '/framework/assets/editor/handler/upload_json.ashx',
                fileManagerJson: '/framework/assets/editor/handler/file_manager_json.ashx',
                allowFileManager: true,
                filterMode: false
            });
        }
        function appendEditorStyle(){
            var ifrs = document.getElementsByClassName('ke-edit-iframe');
            var css = document.createElement('STYLE');
            css.innerHTML='.ke-content p{text-indent:0em;}';

            $JS.each(ifrs,function(i,e){
                e.contentWindow.document.body.appendChild(css);
            });
        }
    </script>
</body>
</html>
